package com.dajie.pt.server

import java.io.{FileInputStream, StringReader}
import java.util.Date

import com.chenlb.mmseg4j.{Word, MMSeg, ComplexSeg, Dictionary}
import com.dajie.pt.server.logger.MMSegLogger


/**
 * Created by yunbinan on 15-4-9.
 */
object MMSegService {
  var dictionary = Dictionary.getInstance("/ROOT/dj-pt-algo-segword-service-dictionary")
  var seg = new ComplexSeg(dictionary)
  val worlSplit: String = "|"

  //分词
  def segWords(input: String): String = {
    MMSegLogger.mmsegLogger.info("input:{}", input)
    var sb = new StringBuilder()
    val mmSeg = new MMSeg(new StringReader(input), seg)
    var word: Word = mmSeg.next()
    var first: Boolean = true
    while (word != null) {
      if (!first) {
        sb.append(worlSplit)
      }
      var str = word.getString()
      sb.append(str)
      first = false
      word = mmSeg.next()
    }
    MMSegLogger.mmsegLogger.info("output:{}", sb.toString())
    sb.toString()
  }

  //重载词库
  def reloadDic(): String = {
    try {
      MMSegLogger.mmsegLogger.info("reload dictionary:{}", dictionary.reload())
      "ok"
    } catch {
      case e: Exception => {
        e.getMessage
      }
    }

  }

  def main(args: Array[String]) {
    println(segWords("java高级开发工程师"))
  }
}
